package com.amk.service;

import java.util.Scanner;

import com.amk.entity.Student;

public class StudentManager {

	Scanner input = new Scanner(System.in);
	//定义10个长度的学生数组
	Student[] students = new Student[10];
	
	//欢迎界面
	public void welcome() {
		System.out.println("欢迎使用学员管理系统");
		System.out.println("制作人：万涛");
		
		System.out.println("1:登录   2:注册   3:找回密码   0:注销");
		
		int choose = input.nextInt();
		
		switch (choose) {
			case 1:
				login();
				break;
			case 2:
			case 3:
			case 0:
				System.out.println("正在研发中...");
				welcome();//返回开始界面
				break;
	
			default:
				System.out.println("您的输入有误，请重新输入！");
				welcome();
				break;
			}
	}
	
	//登录方法
	public void login() {
		System.out.println("账号：");
		String account = input.next();
		System.out.println("密码：");
		String password = input.next();
		
		//账号密码正确
		if(account.equals("wantao")&&password.equals("123456")) {
			menu();
		}else {
			System.out.println("账号或密码错误！请重新输入！");
			login();
		}
		
	}
	
	//菜单
	public void menu() {
		System.out.println("1:添加学员");
		System.out.println("2:显示所有学员");
		System.out.println("3:查找学员");
		System.out.println("4:修改学员");
		System.out.println("5:删除学员");
		System.out.println("0:退出系统");
		
		int choose = input.nextInt();
		
		switch (choose) {
			case 1:
				addStudent();
				break;
			case 2:
				showAllStudent();
				break;
			case 3:
				findStudent();
				break;
			case 4:
				updateStudent();
				break;
			case 5:
				deleteStudent();
				break;
			case 0:
				System.out.println("退出系统...");
				System.exit(0);
				break;
	
			default:
				System.out.println("您的输入有误，请重新输入！");
				menu();
				break;
		}
		
	}
	
	public void addStudent() {
		System.out.println("学号：");
		int stuId = input.nextInt();
		System.out.println("姓名：");
		String name = input.next();
		System.out.println("性别：");
		char sex = input.next().charAt(0);
		System.out.println("年龄：");
		int age = input.nextInt();
		System.out.println("电话：");
		String phone = input.next();
		
		//创建新的学生对象
		Student stu = new Student(stuId, name, sex, age, phone);
		
		//将创建好的学生对象存储起来
		for(int i=0;i<students.length;i++) {
			//循环遍历学生数组，判断哪个位置为null,为null则将学生放在该位置
			if(students[i]==null) {
				students[i] = stu;
				break;
			}
		}
		
		System.out.println("添加学生成功，是否继续添加？Y/N");
		
		String choose = input.next();
		
		if(choose.equals("y")||choose.equals("Y")) {
			addStudent();
		}else {
			menu();
		}
		
	}
	
	public void showAllStudent() {
		System.out.println("学生编号\t姓名\t性别\t年龄\t电话");
		
		for(int i=0;i<students.length;i++) {
			if(students[i]==null) {
				continue;
			}
			Student stu = students[i];
			System.out.println(stu.stuId+"\t"+stu.name+"\t"+stu.sex+"\t"+stu.age+"\t"+stu.phone);
		}
		
		menu();
	}
	
	public void findStudent() {
		System.out.println("请输入您要查找的学员编号：");
		int stuId = input.nextInt();
		//判断当前对象是否存在
		boolean flag = true;
		
		for(int i=0;i<students.length;i++) {
			if(students[i]==null) {
				continue;
			}
			Student stu = students[i];
			if(stu.stuId == stuId) {
				flag = false;
				System.out.println("学生编号\t姓名\t性别\t年龄\t电话");
				System.out.println(stu.stuId+"\t"+stu.name+"\t"+stu.sex+"\t"+stu.age+"\t"+stu.phone);
				break;
			}
		}
		
		//未找到该学生
		if(flag) {
			System.out.println("您查询的学生对象不存在！");
		}
		
		menu();
	
	}
   
	public void updateStudent() {
	System.out.println("请您输入要修改学员的编号：");
	int stuId = input.nextInt();
	boolean flag = true;
	
	for(int i=0;i<students.length;i++) {
		if(students[i]==null) {
			continue;
		}
		Student stu =students[i];
		if(stu.stuId == stuId) {
			flag =false;
			System.out.println("姓名：");
			String name = input.next();
			System.out.println("性别：");
			char sex = input.next().charAt(0);
			System.out.println("年龄：");
			int age = input.nextInt();
			System.out.println("电话：");
			String phone = input.next();
			System.out.println("学生编号\t姓名\t性别\t年龄\t电话");
			System.out.println(stu.stuId+"\t"+stu.name+"\t"+stu.sex+"\t"+stu.age+"\t"+stu.phone);
			break;
			}
	}
	//未找到该学生
	if(flag) {
		System.out.println("您查询的学生对象不存在！");
	}
    System.out.println("修改学生成功，是否继续添加？Y/N");
	
	String choose = input.next();
	
	if(choose.equals("y")||choose.equals("Y")) {
		addStudent();
	}else {
	 menu();}
}

	public void deleteStudent() {
	 System.out.println("请您输入要删除学员编号");
     int stuId = input.nextInt();
		//判断当前对象是否存在
		boolean flag = true;
		
		for(int i=0;i<students.length;i++) {
			if(students[i]==null) {
				continue;
			}
			Student stu = students[i];
			if(stu.stuId == stuId) {
				flag = false;
				 students [i] = null;
				 break;
			}
     
	}
		if(flag) {
			System.out.println("您查询的学生对象不存在！");
		}
        System.out.println("删除学生成功，是否继续添加？Y/N");
		
		String choose = input.next();
		
		if(choose.equals("y")||choose.equals("Y")) {
			addStudent();
		}else {
		 menu();}
}
}

	
